﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace DataAccess
{
    public class DalPlace
    {
        public static List<BusinessLogic.BizPlace> getAll() //Get all stores
        {
            ViMapDataContext db = new ViMapDataContext();
            var places = from pla in db.Places
                         select new BusinessLogic.BizPlace
                         (
                            pla.id,
                            pla.Title,
                            Convert.ToDouble(pla.Latitude),
                            Convert.ToDouble(pla.Longitude),
                            pla.Website,
                            pla.Address,
                            pla.ContactPhone,
                            pla.Icon,
                            pla.Image
                         );
            return places.ToList();
        }

        public static List<BusinessLogic.BizPlace> getSimplyPlaces() //Get stores with name, langitude, longitude
        {
            ViMapDataContext db = new ViMapDataContext();
            var places = from pla in db.Places
                         select new BusinessLogic.BizPlace
                         (
                            pla.id,
                            Convert.ToDouble(pla.Latitude),
                            Convert.ToDouble(pla.Longitude), 
                            pla.Icon
                         );
            return places.ToList();
        }

        public static BusinessLogic.BizPlace getPlacebyId(int id) //Get store by id
        {
            ViMapDataContext db = new ViMapDataContext();
            var place = db.Places.Single(p => p.id == id);
            return new BusinessLogic.BizPlace(place.id, place.Title, Convert.ToDouble(place.Latitude), Convert.ToDouble(place.Longitude), place.Website, place.Address, place.ContactPhone, place.Icon, place.Image);
        }

        public static List<BusinessLogic.BizPlace> searchPlace(string address, int cityId) //Search stores with address parameter
        {
            ViMapDataContext db = new ViMapDataContext();
            List<BusinessLogic.BizPlace> places = DalCity.getPlaceinCity(cityId);
            if (address.Trim() != "")
            {
                string add = BizTVHam.ConvertToUnSign(address).ToLower();
                places = places.FindAll(
                                            delegate(BusinessLogic.BizPlace place)
                                            {
                                                if (BizTVHam.ConvertToUnSign(place.address.ToLower()).Contains(add) || BizTVHam.ConvertToUnSign(place.title.ToLower()).Contains(add))
                                                    return true;
                                                else
                                                    return false;
                                            });
            }

            return places;
        }
    }
}